﻿using StoryTeller.Domain;
using StoryTeller.Infrastructure.DataAccess.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StoryTeller.Infrastructure.DataAccess.Parser
{
    public static class GroupParser
    {        
        public static Group ParseEntityToDomain(this EGroup entity)
        {
            Group grp = new Group();

            grp.Id = entity.Id;
            grp.IsPublic = entity.IsPublic;
            grp.LastUpdate = entity.LastUpdate;
            grp.CreationDate = entity.CreationDate;
            grp.Title = entity.Title;

            grp.Owner = new User() { Id = entity.OwnerId };
            grp.ContributorsRequests = entity.GroupContributorRequests == null ? null : entity.GroupContributorRequests.Select(c => c.ParseEntityToDomain()).ToList();
            grp.Contributors = entity.Users == null ? null : entity.Users.Select(c => UserParser.ParseEntityToDomain(c)).ToList();

            return grp;
        }

        public static EGroup ParseDomainToEntity(this Group domain)
        {            
            EGroup grp = new EGroup();

            grp.Id = domain.Id;
            grp.IsPublic = domain.IsPublic;
            grp.CreationDate = domain.CreationDate;
            grp.LastUpdate = domain.LastUpdate;
            grp.Title = domain.Title;
            grp.OwnerId = domain.Owner == null ? default(int) : domain.Owner.Id;
            grp.GroupContributorRequests = domain.ContributorsRequests == null ? null : domain.ContributorsRequests.Select(c => GroupContributorRequestParser.ParseDomainToEntity(c)).ToList();
            grp.Users = domain.Contributors == null ? null : domain.Contributors.Select(c => UserParser.ParseDomainToEntity(c)).ToList();

            return grp;
        }
    }
}
